package com.example.hfiveandaplication.common;

import android.app.Dialog;


import java.util.Stack;

public class DialogManager {
    private Stack<Dialog> dialogStack;
    private static DialogManager instance;

    private int updateVersionWeight = 1;
    private int normalWeight = 0;

    public static DialogManager getManager() {
        if (instance == null) {
            instance = new DialogManager();
        }

        return instance;
    }

    /**
     * 添加activity
     *
     * @param dialog
     */
    public void showDialogStack(Dialog dialog) {
        if (dialogStack == null) {
            dialogStack = new Stack<Dialog>();
        }

        if (dialogStack.empty()) {
            dialog.show();
            dialogStack.add(dialog);
        } else if (getDialogWeight(dialog) > getDialogWeight(currentDialog())){
            currentDialog().dismiss();
            dialog.show();
            dialogStack.add(0, dialog);
        } else {
            dialogStack.add(dialog);
        }
    }

    /**
     * 获取当前的dialog
     */
    public Dialog currentDialog() {
        return dialogStack.empty() ? null : dialogStack.lastElement();
    }

    public void dismissDialog(Dialog dialog) {
        if (dialog != null) {
            dialogStack.remove(dialog);
            dialog.dismiss();
            if (!dialogStack.empty()) {
                currentDialog().show();
            }
        }
    }

    public void dismissDialog() {
        Dialog dialog = dialogStack.lastElement();
        dismissDialog(dialog);
    }

    public void dismissAllDialog() {
        for (Dialog dialog : dialogStack) {
            if (null != dialog) {
                dismissDialog(dialog);
            }
        }
    }

    /**
     * 获取对话框权重
     */
    public int getDialogWeight(Dialog dialog) {

        return normalWeight;
    }

}
